"""
Since the collection class hasn't been added to clir directory. A list
is now used to store AlignedSent objects

Author: Baolin Jing, Kai Chen
"""


from aligned_corpus_reader import *
from aligned_sent import *
from aligned_sent_collection import *


def GizaReader(root, fileids):
    """
    This wrapper function accepts file path and file IDs and returns a list
    of AlignedSent objects extracted from the files specified.
    Example of usage:
      >>> aligned_sents_giza = GizaReader('../data/giza/', 'alignment-en-fr')

    @param root: the path for giza files
    @type root: C{str}
    @param fileids: the names of giza files
    @type fileids: C{list} of C{str}
    """
    
    giza = GizaCorpusReader(root, fileids)
    aligned_sents_giza = AlignedSentCollection()

    for sent in giza.aligned_sents():
        aligned_sents_giza.add(sent)

    return aligned_sents_giza


def NaaclReader(root, fileids):
    """
    This wrapper function accepts file path and file IDs and returns a list
    of AlignedSent objects extracted from the files specified.
    Example of usage:
      >>> aligned_sents_naacl = NaaclReader('../data/naacl/', \
          ['trial.e', 'trial.f', 'trial.wa'])

    @param root: the path for naacl files
    @type root: C{str}
    @param fileids: the names of naacl files
    @type fileids: C{list} of C{str}
    """
    text_fileid1 = fileids[:1]
    text_fileid2 = fileids[1:2]
    align_fileid = fileids[(len(fileids) - 1):]

    alignment = []
    possible_alignment = []
    aligned_sents_naacl = AlignedSentCollection()

    naacl_text1 = NaaclCorpusReader(root, text_fileid1, 'text')
    naacl_text2 = NaaclCorpusReader(root, text_fileid2, 'text')
    naacl_align = NaaclCorpusReader(root, align_fileid, 'align')

    index = 0 ## not sure why i can't do naacl_align.sents().index(sent)
    for sent in naacl_align.sents():
        
        p_align_sent = []
        all_align_sent = []
        align_sent = naacl_align2tuple(sent)
        for align in align_sent:
            if align[2] == 'P':
                p_align_sent.append((align[0], align[1]))
                all_align_sent.append((align[0], align[1]))
            else:
                all_align_sent.append((align[0], align[1]))

        alignment.append(all_align_sent)
        possible_alignment.append(p_align_sent)

        aligned_sent = AlignedSent(naacl_text1.sents()[index], \
                            naacl_text2.sents()[index], alignment[index], \
                            possible_alignment[index])
        aligned_sents_naacl.add(aligned_sent)
        index += 1

    return aligned_sents_naacl
        

def naacl_align2tuple(sent):
    """
    change alignment format from strings to tuples,
    for example from '1-1-P' to (1,1,P)

    @param sent: alignments in a sentence
    @type sent: C{list} of C{str}
    """
    align_tuples = []
    for pair in sent:
        i, j, p = pair.split('-')
        align_tuples.append((int(i), int(j), p))

    return align_tuples

